#include "richerpluginwidget.h"
#include "ui_richerpluginwidget.h"
#include "richerplugin.h"

richerpluginwidget::richerpluginwidget(PluginConfigBase* cfg, QWidget *parent):
    ConfigWidgetBase(cfg, parent),
    ui(new Ui::richerpluginwidget)
{
    ui->setupUi(this);

    showInfo();
}

richerpluginwidget::~richerpluginwidget()
{
    delete ui;
}


void richerpluginwidget::applyConfigs()
{
    RicherConfig* f = (RicherConfig*)cfg;
    f->doBuy = ui->cbBuy->isChecked();
    f->doSell = ui->cbSell->isChecked();
}

void richerpluginwidget::showInfo()
{
    RicherConfig* f = (RicherConfig*)cfg;
    ui->cbBuy->setChecked(f->doBuy);
    ui->cbSell->setChecked(f->doSell);
    ui->tbInfo->verticalHeader()->setDefaultSectionSize(24);
    ui->tbInfo->verticalHeader()->hide();
    ui->tbInfo->setColumnWidth(0, 100);
    ui->tbInfo->setColumnWidth(1, 100);
    ui->tbInfo->setColumnWidth(2, 100);
    ui->tbInfo->setColumnWidth(3, 100);

    ui->tbInfo->clearContents();
    RicherPlugin* plugin = (RicherPlugin*)(cfg->plugin);
    QMap<int, Good*>::Iterator it = plugin->goodMap.begin();
    int index = 0;
    while(it != plugin->goodMap.end())
    {
        Good* good = it.value();
        if(!good)
        {
            ++it;
            continue;
        }

        long long counts = 0;
        QMap<long long, long long>::Iterator itc = good->counts.begin();
        while(itc != good->counts.end())
        {
            counts += itc.value();
            ++itc;
        }

        QString prices;
        QString percents;
        double intrest = 0;
        QList<long long>::Iterator itp = good->prices.begin();
        while(itp != good->prices.end())
        {
            long long price = *itp;
            long long count = good->counts.value(price);
            if(!prices.isEmpty())
                prices += ",";
            if(!percents.isEmpty())
                percents += ",";
            prices += QString::number(price);
            percents += QString::number((double)count/counts*100, 'f', 1) + "%";
            if(good->price != 0)
            {
                intrest += (price - good->price) * ((double)count/counts);
            }
            ++itp;
        }

        ui->tbInfo->insertRow(ui->tbInfo->rowCount());
        QTableWidgetItem* hi;
        hi = new QTableWidgetItem(good->name);
        ui->tbInfo->setItem(index, 0, hi);

        hi = new QTableWidgetItem(QString::number(good->price));
        ui->tbInfo->setItem(index, 1, hi);

        hi = new QTableWidgetItem(QString::number(intrest, 'f', 2));
        ui->tbInfo->setItem(index, 2, hi);

        if(good->price>0)
        {
            hi = new QTableWidgetItem(QString::number(intrest/good->price*100, 'f', 1) + "%");
            ui->tbInfo->setItem(index, 3, hi);
        }


        ++it;++index;
    }

}

void richerpluginwidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
